package huaweiod.second;

import java.util.Scanner;

/**
 * 中庸行者
 * https://renjie.blog.csdn.net/article/details/135158705
 */
public class C8_Moderate {

    public static int[][] dir = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = in.nextInt();
            }
        }

        boolean[][] visit;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < dir.length; k++) {
                    visit = new boolean[n][m];
                    visit[i][j] = true;
                    int dfs = dfs(i + dir[k][0], j + dir[k][1], i, j, visit, matrix);
                    ans = Math.max(ans, dfs);
                }
            }
        }
        System.out.println(ans);
    }

    private static int dfs(int i, int j, int preX, int preY, boolean[][] visit, int[][] matrix) {
        if (i < 0 || i == matrix.length || j < 0 || j == matrix[0].length
                || visit[i][j] || matrix[i][j] >= matrix[preX][preY]) {
            return 0;
        }
        int path = 0;
        visit[i][j] = true;
        for (int k = 0; k < dir.length; k++) {
            int dfs = dfs(i + dir[k][0], j + dir[k][1], i, j, visit, matrix);
            path = Math.max(path, dfs);
        }
        visit[i][j] = false;
        return path + 1;
    }
}
